#include "kernel/syscall/syscall.h"
#include "libs/msr.h"
#include "kernel/log/log.h"
#include "libs/lib.h"
#include "kernel/task/task.h"
#include "kernel/syscall/unistd.h"
#include "libs/errno.h"
#include "kernel/syscall/sys.h"

#define table  SYSCALLMANAGER->system_call_table


extern "C" void system_call(void);


#define SYSCALL_COMMON(table,nr,sym)	extern "C" uint64_t sym(StackFrame* regs);


#include "kernel/syscall/syscalls.h"

#undef	SYSCALL_COMMON


#define SYSCALL_COMMON(test,nr,sym)	test[nr] = sym;





//=========================================systemcallmanager==============

systemcallmanager* systemcallmanager::systemcallm = 0;

systemcallmanager::systemcallmanager(/* args */)
{
    
    this->systemcallm = this;
    wrmsr(IA_32_SYSENTER_CS,KERNEL_CS);
    wrmsr(IA32_SYSENTER_ESP,current->thread->rsp0);
    wrmsr(IA32_SYSENTER_EIP,(uint64_t)system_call);
    

    for (uint16_t i = 0; i < MAX_SYSTEM_CALL_NR; i++)
    {
        system_call_table[i] = no_system_call;
    }

    #include "kernel/syscall/syscalls.h"

}

systemcallmanager::~systemcallmanager()
{
}



uint64_t systemcallmanager::HandleSystemCall(StackFrame* regs){

    return  SYSCALLMANAGER->system_call_table[regs->RAX](regs);

}


